var FindJob = {
	InitBeijing: function(place){
		$ajax({
			url: "/jobs/bj",
			data:{
				to: CurrentUser._id,
				place:0
			},
			success: function(res){
				var data = res.result;
				var html = '';
				SetLS("myJobs0",data);
				if(!data||data.length==0||data==undefined){
                   html += FindJob.getCityHtml();  
                }else{
                    $.each(data,function(i,item){
                    html += FindJob.getMyHtml(0,i,item.name,item.from,item.post,item.create,item.require,item.natrure,item.phone,item.photo);  
                });
                }
				$("#beiJing").html(html);
			}
		});
	},
	InitShanghai: function(place){
        $ajax({
            url: "/jobs/sh",
            data:{
                to: CurrentUser._id,
                place:1
            },
            success: function(res){
                var data = res.result;
                var html = '';
                SetLS("myJobs1",data);
                if(!data||data.length==0||data==undefined){
                   html += FindJob.getCityHtml();  
                }else{
                    $.each(data,function(i,item){
                    html += FindJob.getMyHtml(1,i,item.name,item.from,item.post,item.create,item.require,item.natrure,item.phone,item.photo);  
                });
                }
                $("#shangHai").html(html);
            }
        });
    },
    InitGuangzhou: function(place){
        $ajax({
            url: "/jobs/gz",
            data:{
                to: CurrentUser._id,
                place:2
            },
            success: function(res){
                var data = res.result;
                var html = '';
                SetLS("myJobs2",data);
                if(!data||data.length==0||data==undefined){
                   html += FindJob.getCityHtml();  
                }else{
                    $.each(data,function(i,item){
                    html += FindJob.getMyHtml(2,i,item.name,item.from,item.post,item.create,item.require,item.natrure,item.phone,item.photo);  
                });
                }
               $("#guangZhou").html(html);
            }
        });
    },
    InitShenzhen: function(place){
        $ajax({
            url: "/jobs/sz",
            data:{
                to: CurrentUser._id,
                place:3
            },
            success: function(res){
                var data = res.result;
                var html = '';
                SetLS("myJobs3",data);
                if(!data||data.length==0||data==undefined){
                   html += FindJob.getCityHtml();  
                }else{
                    $.each(data,function(i,item){
                    html += FindJob.getMyHtml(3,i,item.name,item.from,item.post,item.create,item.require,item.natrure,item.phone,item.photo);  
                });
                }
                $("#shenZhen").html(html);
            }
        });
    },
    InitOthers: function(place){
        $ajax({
            url: "/jobs/qt",
            data:{
                to: CurrentUser._id,
                place:4
            },
            success: function(res){
                var data = res.result;
                var html = '';
                SetLS("myJobs4",data);
                if(!data||data.length==0||data==undefined){
                   html += FindJob.getCityHtml();  
                }else{
                    $.each(data,function(i,item){
                    html += FindJob.getMyHtml(4,i,item.name,item.from,item.post,item.create,item.require,item.natrure,item.phone,item.photo);  
                });
                }
                $("#others").html(html);
            }
        });
    },
    getCityHtml:function(){
         return '<div class="">'+
                    '<div class="tx-c umt_p60">'+(CurrentUser.category==30?"亲,当前城市还没有工作机会信息哦！":"There is no Jobs at this moment!")+'</div>'+
                 '</div>';  
    },
	getJobHtml: function(i,title,create,content){
		return 	'<div class="ub ub-ver ubb b_gra upp5" ontouchstart="zy_touch()" onclick="FindJob.jumpJobDetail(\'' + i + '\')">' + 
                '   <div class="ub">' + 
                '       <div class="ub ub-ac ub-f1 upt_p5 upl_p5 uf1p1 ut-s">' + title + '</div>' + 
                '       <div class="ub ub-ac upt_p5 upl_p5 upr_p5 ufp8 c_org">' + (new Date(create)).pattern('yyyy-MM-dd') + '</div>' + 
                '   </div>' + 
                '   <div class="upp5 ufp9 ut-s">' + content + '</div>' + 
                '</div>';
	},
	getMyHtml: function(index,i,name,from,post,create,require,natrure,phone,photo){
		var touchEvent = (CurrentUser._id == from)?"FindJob.jumpMyDetail('" + index + "','"+i+"')":"FindJob.jumpOtherDetail('" + index + "','"+i+"')";
      return    ' <div class="ub up_list4 detailEvt " ontouchstart="zy_touch()" onclick='+touchEvent+'>'+
                '<div class="uw5 uh5 ub_img" style="background-image:url('+ Core.id2url(photo) +')"></div>'+
                '     <div class="ub ub-ver umt_1p5 pso_abs">'+name+'</div>'+ 
                '            <div class="ub umt_5 uml_4f umb_p3">'+             
                '             <div class="ub-img uw1p2 uh1p2 im_set_icon9"></div>'+              
                '            <div class="ub ub-ver pso_abs">'+natrure+'</div>'+          
                '           <div class="ub-img uw1p5 uh1p2 im_icon_phone_no uml_6p5"></div>'+              
                '          <div class="ub ub-ver pso_abs uw10">'+phone+'</div>'+
                '         <div class="upp5 ufp8 ut-s gray2 upr_p5 pso_abs ub_right uml_15m6 ub-f1 ub-ae ub-pe">'+(new Date(create)).pattern('yyyy-MM-dd')+'</div>'+       
                '      </div>'+
                '      <div class="ub upp5 uw10w5 pso_abs ub_right umt_1 uml_6p3 ub-f1 upr_p5 ub-ae ub-pe">'+require+'</div>'+ 
                '</div>';
	},
	jumpJobDetail: function(i){
		var item = GetLS("jobs")[i];
		SetLS("job",item);
		SetLS("newperson_detail",{name:item.fromName,from:item.from});
		OpenNewWin("findjob_jobDetail");
	},
	jumpMyDetail: function(index,i){
		var item = "";
        if(index == "0"){
            item = GetLS("myJobs0")[i];
        }
        if(index == "1"){
            item = GetLS("myJobs1")[i];
        }
        if(index == "2"){
            item = GetLS("myJobs2")[i];
        }
        if(index == "3"){
            item = GetLS("myJobs3")[i];
        }
        if(index == "4"){
            item = GetLS("myJobs4")[i];
        }
        
        SetLS("myJob",item);
        OpenNewWin("findjob_myEdit");
	},
	jumpOtherDetail: function(index,i){
		var item = "";
        if(index == "0"){
            item = GetLS("myJobs0")[i];
        }
        if(index == "1"){
            item = GetLS("myJobs1")[i];
        }
        if(index == "2"){
            item = GetLS("myJobs2")[i];
        }
        if(index == "3"){
            item = GetLS("myJobs3")[i];
        }
        if(index == "4"){
            item = GetLS("myJobs4")[i];
        }
        SetLS("myJob",item);
        SetLS("newperson_detail",{from:item.from});
        OpenNewWin("findjob_otherDetail");
	},
	jobDetailInit: function(){
		var data = GetLS("job");
		$(".value").each(function(){
			$(this).text(data[$(this).attr("id")]);
		});
		$("#create").text( (new Date(data.create)).pattern('yyyy-MM-dd HH:mm:ss'));
		
		$.each(data.photo,function(i,item){
			$("#photoList").append('<div data-photo="set_bg.png" class="uw100 umauto uptb_p5 photo"><img src="' + Core.id2url(item,"photo") + '" class="uw100"></div>');
		});
	},
	myDetailInit: function(){
		var data = GetLS("myJob");
		$(".value").each(function(i){
			$(this).val(data[$(this).attr("id")]);
		});
		var cu=CurrentUser.category;
		if(cu==30){
		   $("#place").text(data.place);
		         //申请城市
                    if(data.place==0){
                       $("#place").text("北京");
                       $("#place").attr("data-num",data.place);
                    }
                    if(data.place==1){
                       $("#place").text("上海");
                       $("#place").attr("data-num",data.place);
                    }
                    if(data.place==2){
                       $("#place").text("广州");
                       $("#place").attr("data-num",data.place);
                    }
                    if(data.place==3){
                       $("#place").text("深圳");
                       $("#place").attr("data-num",data.place);
                    }
                    if(data.place==4){
                       $("#place").html('<input id="others" type="text" class="ub ub-f1 uc-a1 input_sz value" placeholder="Please Input"/>');
                       $("#others").val(data.placeOther);
                       $("#place").attr("data-num",data.place);
                    }
                 //申请岗位   
                    if(data.post=="English Teacher"){
                       $("#post").text("英语老师");
                    }
                    else if(data.post=="DJ"){
                       $("#post").text("DJ");
                    }
                    else if(data.post=="Model"){
                       $("#post").text("模特");
                    }
                    else if(data.post=="Writer&Editor"){
                       $("#post").text("编辑&写作");
                    }
                    else if(data.post=="Marketing Manager"){
                       $("#post").text("市场拓展经理");
                    }
                    else {
                       $("#post").html('<input id="post1" type="text" class="ub ub-f1 uc-a1 input_sz" placeholder="Please Input"/>'); 
                        $("#post1").val(data.post);
                    } 
                  
                  //岗位性质  
                    if(data.natrure=="Full Time"){
                       $("#natrure").text("全职");
                    }
                    if(data.natrure=="Part Time"){
                       $("#natrure").text("兼职");
                    }
                  
                  //申请需求
                    if(data.require=="Recruitment"){
                       $("#require").text("招聘");
                    }
                    if(data.require=="Apply for a job"){
                       $("#require").text("应聘");
                    }
                    
                 //中文水平
                    if(data.level=="Low"){
                       $("#level").text("低");
                    }
                    if(data.level=="Middle"){
                       $("#level").text("中");
                    }
                    if(data.level=="High"){
                       $("#level").text("高");
                    }
		}else{
		                     
                    if(data.place==0){
                       $("#place").text("BeiJing");
                       $("#place").attr("data-num",data.place);
                    }
                    if(data.place==1){
                       $("#place").text("ShangHai");
                       $("#place").attr("data-num",data.place);
                    }
                    if(data.place==2){
                       $("#place").text("GuangZhou");
                       $("#place").attr("data-num",data.place);
                    }
                    if(data.place==3){
                       $("#place").text("ShenZhens");
                       $("#place").attr("data-num",data.place);
                    }
                    if(data.place==4){
                        $("#place").html('<input id="others" type="text" class="ub ub-f1 uc-a1 input_sz value" placeholder="Please Input"/>');
                       $("#others").val(data.placeOther);
                       $("#place").attr("data-num",data.place);
                    }
                    
                    //申请岗位   
                    if(data.post=="English Teacher"){
                       $("#post").text(data.post);
                    }
                    else if(data.post=="DJ"){
                       $("#post").text(data.post);
                    }
                    else if(data.post=="Model"){
                       $("#post").text(data.post);
                    }
                    else if(data.post=="Writer&Editor"){
                       $("#post").text(data.post);
                    }
                    else if(data.post=="Marketing Manager"){
                       $("#post").text(data.post);
                    }
                    else {
                        $("#post").html('<input id="post1" type="text" class="ub ub-f1 uc-a1 input_sz" placeholder="Please Input"/>'); 
                        $("#post1").val(data.post);
                    } 
                    
                    
		}
        // $("#post").text(data.post);
        // $("#natrure").text(data.natrure);
        // $("#require").text(data.require);
        // $("#level").text(data.level);
        $("#create").text(new Date(data.create).pattern("yyyy-MM-dd HH:mm:ss"));
	},
	otherDetailInit: function(){
		var data = GetLS("myJob");
		$(".value").each(function(i){
			$(this).text(data[$(this).attr("id")]);
		});
		var cu=CurrentUser.category;
        if(cu==30){
           $("#place").text(data.place);
                 //申请城市
                    if(data.place==0){
                       $("#place").text("北京");
                    }
                    if(data.place==1){
                       $("#place").text("上海");
                    }
                    if(data.place==2){
                       $("#place").text("广州");
                    }
                    if(data.place==3){
                       $("#place").text("深圳");
                    }
                    if(data.place==4){
                        $("#place").text(data.placeOther);
                    }
                 //申请岗位   
                    if(data.post=="English Teacher"){
                       $("#post").text("英语老师");
                    }
                    else if(data.post=="DJ"){
                       $("#post").text("DJ");
                    }
                    else if(data.post=="Model"){
                       $("#post").text("模特");
                    }
                    else if(data.post=="Writer&Editor"){
                       $("#post").text("编辑&写作");
                    }
                    else if(data.post=="Marketing Manager"){
                       $("#post").text("市场拓展经理");
                    }
                    else {
                        $("#post").text(data.post);
                    } 
                  
                  //岗位性质  
                    if(data.natrure=="Full Time"){
                       $("#natrure").text("全职");
                    }
                    if(data.natrure=="Part Time"){
                       $("#natrure").text("兼职");
                    }
                  
                  //申请需求
                    if(data.require=="Recruitment"){
                       $("#require").text("招聘");
                    }
                    if(data.require=="Apply for a job"){
                       $("#require").text("应聘");
                    }
                    
                 //中文水平
                    if(data.level=="Low"){
                       $("#level").text("低");
                    }
                    if(data.level=="Middle"){
                       $("#level").text("中");
                    }
                    if(data.level=="High"){
                       $("#level").text("高");
                    }
        }else{
                             
                    if(data.place==0){
                       $("#place").text("BeiJing");
                    }
                    if(data.place==1){
                       $("#place").text("ShangHai");
                    }
                    if(data.place==2){
                       $("#place").text("GuangZhou");
                    }
                    if(data.place==3){
                       $("#place").text("ShenZhens");
                    }
                    if(data.place==4){
                        $("#place").text(data.placeOther);
                    }
                    
                    //申请岗位   
                    $("#post").text(data.post);
                   
                    
        }
        $("#create").text(new Date(data.create).pattern("yyyy-MM-dd HH:mm:ss"));
	},
	selectSheet: function(tab){
		var arr = "";
		switch(tab){
			case "post":
			    //if(CurrentUser.category==30){
			    //  arr = jobArr;  
			   // }else{
			      arr = jobArr2;
			   // }
				break;
			case "place":
			   //if(CurrentUser.category==30){
				// arr = addressArr;
				//}else{
				 arr = addressArr1;  
				//}
				break;
			case "natrure":
			  // if(CurrentUser.category==30){
               //   arr = jobArr4; 
               //  }else{
                  arr = jobArr3;    
               //  } 
                break;
            case "require":
                // if(CurrentUser.category==30){
               //   arr = jobArr4;  
                // }else{
                  arr = jobArr5;   
                // }
                break;
            case "level":
               // if(CurrentUser.category==30){
               //   arr = jobArr6;  
               // }else{
                  arr = jobArr7;
                //}
                break;
		}
		ActionSheet(CurrentUser.category==30?"请选择":"Please Check", CurrentUser.category==30?"取消":"Cancel", arr, function(data) {
			//$("#" + tab).text(arr[data]).data("index",data);
			
			if(tab == "place"){
                if(data==4){
                    $("#" + tab).attr("data-num",data);
                    $("#" + tab).html('<input id="others" type="text" class="ub ub-f1 uc-a1 input_sz value" placeholder="Please Input"/>');            
                }else{
                    $("#" + tab).attr("data-num",data);
                    $("#" + tab).text(arr[data]);
                }
            }else if(tab == "post"){
                if(data==5){
                    $("#" + tab).attr("data-post",arr[data]);
                    $("#" + tab).html('<input id="post1" type="text" class="ub ub-f1 uc-a1 input_sz" placeholder="Please Input"/>');            
                }else{
                    $("#" + tab).attr("data-post",arr[data]);
                    $("#" + tab).text(arr[data]);
                }
            }else{
                    $("#" + tab).attr("data-num",data);
                    $("#" + tab).text(arr[data]);
            }
			
		});
	},
	uploadPhoto: function(){
		Upload.init(Core.API_VER + "/upload", function(res) {
			res = res[0];
		 	if (res.code) {
		 		$alert(res.codemsg);
		 		return;
		 	}                
            var url = res.result.url; 
            $("#photoList").append('<div data-photo="' + url + '" class="uw70 umauto photo"><img src="' + Core.IMG_VER + url + '" class="uw100"><img src="../img/icon_close.png" style="width:2em;height:2em;z-index:99999;position:absolute;top:-1em;right:-1em;" onclick="FindJob.closePhoto();"></div>');
		});
	},
	closePhoto: function(){
		var $this = $(event.currentTarget);
		$this.parent().remove();
	},
	add: function(){
		var data = new Object();
        $("body").find(".value").each(function(e){
            data[$(this).attr("id")] = $(this).val();
        });

        data.from = CurrentUser._id;
        //data.place = $("#place").text();
        data.place = $("#place").data("num");
        data.post = $("#post").data("post");
        if(data.post == "Others"){
            data.post = $("#post1").val();
        }
        data.natrure = $("#natrure").text();
        data.require = $("#require").text();
        data.level = $("#level").text();
        if(data.place==4){
           data.placeOther = $("#others").val();
        }
        if(!IsDefine($("#name").val())){
            return (CurrentUser.category==30?$alert("申请人不能为空！"):$alertEn("The Applicant is empty!"));
        }
        if(!IsDefine($("#phone").val())){
            return (CurrentUser.category==30?$alert("联系电话不能为空！"):$alertEn("The Mobile Phone is empty!"));
        }
        if(!IsDefine($("#country").val())){
            return (CurrentUser.category==30?$alert("国籍不能为空！"):$alertEn("The From is empty!"));
        }
        if(!IsDefine($("#unit").val())){
            return (CurrentUser.category==30?$alert("现在工作不能为空！"):$alertEn("The Current Job is empty!"));
        }
        if(data.place==4){
           if(!IsDefine($("#others").val())){
            return (CurrentUser.category==30?$alert("申请城市不能为空！"):$alertEn("The Apply City is empty!"));
           }else{
              data.placeOther = $("#others").val(); 
           } 
        }
        if($("#post").data("post")== "Others"){
           if(!IsDefine($("#post1").val())){
             return (CurrentUser.category==30?$alert("申请岗位不能为空！"):$alertEn("The Apply Position is empty!"));
            }else{
               data.post = $("#post1").val();
            }
        }
        $ajax({
            url: "/jobs/upsert",
            data: data,
            success: function(res){
                var ml=CurrentUser.category==30?"ch/":"en/";
                SetLS("myJob",res.result);
                PCall("../"+ml+"findjob","CCall('FindJob.InitBeijing()')");
                PCall("../"+ml+"findjob","CCall('FindJob.InitShanghai()')");
                PCall("../"+ml+"findjob","CCall('FindJob.InitGuangzhou()')");
                PCall("../"+ml+"findjob","CCall('FindJob.InitShenzhen()')");
                PCall("../"+ml+"findjob","CCall('FindJob.InitOthers()')");
                PCall("findjob_add","WinClose()");
            }
        });
	},
	prependMyAdd: function(){
		var item = GetLS("myJob");
		var items = GetLS("myJobs");
		var i = items.length;
		items.push(item);
		SetLS("myJobs",items);
		$("#historyList").prepend(FindJob.getMyHtml(i,item.from,item.post,item.create,item.require,item.place,item.natrure,item.pay));
	},
	edit: function(){
		var data = new Object();
		data._id = GetLS("myJob")._id;
		data.from = CurrentUser._id;
		$("body").find(".value").each(function(e){
            data[$(this).attr("id")] = $(this).val();
        });
        data.place = $("#place").data("num");
        if(data.place==4){
           data.placeOther = $("#others").val();
        }
        data.post = $("#post").data("post");
        if(data.post == "Others"){
            data.post = $("#post1").val();
        }
        data.post = $("#post1").val();
        data.natrure = $("#natrure").text();
        data.require = $("#require").text();
        data.level = $("#level").text();
        if(data.place==4){
           $("#place").text($("#place").val());
        }
        if(!IsDefine($("#name").val())){
            return (CurrentUser.category==30?$alert("申请人不能为空！"):$alertEn("The Applicant is empty!"));
        }
        if(!IsDefine($("#phone").val())){
            return (CurrentUser.category==30?$alert("联系电话不能为空！"):$alertEn("The Mobile Phone is empty!"));
        }
        if(!IsDefine($("#country").val())){
            return (CurrentUser.category==30?$alert("国籍不能为空！"):$alertEn("The From is empty!"));
        }
        if(!IsDefine($("#unit").val())){
            return (CurrentUser.category==30?$alert("现在工作不能为空！"):$alertEn("The Current Job is empty!"));
        }
        // if(data.place==4){
           // if(!IsDefine($("#others").val())){
            // return (CurrentUser.category==30?$alert("申请城市不能为空！"):$alertEn("The Apply City is empty!"));
           // }else{
              // data.placeOther = $("#others").val(); 
           // } 
        // }
        // if($("#post").data("post")== "Others"){
           // if(!IsDefine($("#post1").val())){
             // return (CurrentUser.category==30?$alert("申请岗位不能为空！"):$alertEn("The Apply Position is empty!"));
            // }else{
               // data.post = $("#post1").val();
            // }
        // }
		$ajax({
			url: "/jobs/upsert",
			data: data,
			success: function(res){
			    var ml=CurrentUser.category==30?"ch/":"en/";
				SetLS("myJob",data);
				PCall("../"+ml+"findjob","CCall('FindJob.InitBeijing()')");
                PCall("../"+ml+"findjob","CCall('FindJob.InitShanghai()')");
                PCall("../"+ml+"findjob","CCall('FindJob.InitGuangzhou()')");
                PCall("../"+ml+"findjob","CCall('FindJob.InitShenzhen()')");
                PCall("../"+ml+"findjob","CCall('FindJob.InitOthers()')");
				PCall("findjob_myEdit","WinClose()");
			}
		});
	},
	remove: function() {
  		uexWindow.confirm(CurrentUser.category==30?"警告":"Notice",CurrentUser.category==30?"确定要删除吗？":"Do you want to detete?",CurrentUser.category==30?"确定,取消":"OK,Cancel");
  			function cbConfirm(opId, dataType, ensure){
            if(ensure == 0){
  			var job = GetLS("myJob");
  			$ajax({
				url: "/jobs/remove",
				data: {_id: job._id},
				success: function(res) {
					// if(res.code)
						// return $alert(res.codemsg);
                var ml=CurrentUser.category==30?"ch/":"en/";
                PCall("../"+ml+"findjob","CCall('FindJob.InitBeijing()')");
                PCall("../"+ml+"findjob","CCall('FindJob.InitShanghai()')");
                PCall("../"+ml+"findjob","CCall('FindJob.InitGuangzhou()')");
                PCall("../"+ml+"findjob","CCall('FindJob.InitShenzhen()')");
                PCall("../"+ml+"findjob","CCall('FindJob.InitOthers()')");
			    PCall("findjob_myEdit","WinClose()");
				}
			});
  		  }
  		}
  		uexWindow.cbConfirm = cbConfirm; 
	},	
}